﻿<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <!--作为公共模版分离出去-->
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="Bookmark" href="admin/images/rookieCs.ico" />
    <link rel="Shortcut Icon" href="admin/images/rookieCs.ico" />
    <link rel="stylesheet" type="text/css" href="admin/static/h-ui/css/H-ui.my.css" />
    <link rel="stylesheet" type="text/css" href="admin/static/h-ui.admin.pro.iframe/css/h-ui.admin.pro.iframe.min.css" />
    <link rel="stylesheet" type="text/css" href="admin/lib/Hui-iconfont/1.0.9/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="admin/static/h-ui.admin.pro.iframe/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="admin/lib/jquery-easyui/1.7.0/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="admin/lib/jquery-easyui/1.7.0/themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="admin/lib/lc_switch/20160127/css/lc_switch.css" />
    <link rel="stylesheet" type="text/css" href="admin/css/iconfont/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="admin/css/mywork.css" />
    <script type="text/javascript" src="admin/lib/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="admin/lib/layer/3.1.1/layer.js"></script>
    <script type="text/javascript" src="admin/lib/jquery-easyui/1.7.0/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="admin/lib/jquery-easyui/1.7.0/plugins/datagrid-detailview.js"></script>
    <script type="text/javascript" src="admin/lib/jquery-easyui/1.7.0/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="admin/lib/lc_switch/20160127/js/lc_switch.min.js"></script>
    <script>document.write('<script type="text/javascript" src="admin/js/common.js?v='+ Date.now() +'"><\/script>');</script>
    <script>document.write('<script type="text/javascript" src="admin/js/formatter.js?v='+ Date.now() +'"><\/script>');</script>
    <!--作为公共模版分离出去-->
    <title>我的客户信息</title>
</head>
<body class="easyui-layout" style="width: 100%; height: 100%;font-family: 微软雅黑,serif" memuId="customer-list">
<nav class="breadcrumb">
    <i class="Hui-iconfont">&#xe67f;</i> 首页
    <span class="c-gray en" style="float: none">&gt;</span> 客户管理
    <span class="c-gray en" style="float: none">&gt;</span> 客户列表
    <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a>
</nav>
<div data-options="region:'center', border:false">
    <!--按钮元素-->
    <div id="buttonInfo" style="padding: 2px 0px 2px 5px; background-color: #F5FAFE;">
        <button class="easyui-linkbutton" onclick="query(pageParam, true)"><i class="Hui-iconfont">&#xe665;</i>查询</button>
        <button class="easyui-linkbutton" onclick="addCustomer()"><i class="Hui-iconfont">&#xe600;</i>新增</button>
        <button class="easyui-linkbutton" onclick="editCustomer()"><i class="Hui-iconfont">&#xe6df;</i>修改</button>
        <button class="easyui-linkbutton" onclick="delCustomer()"><i class="Hui-iconfont">&#xe6e2;</i>删除</button>
    </div>
    <!--表格元素-->
    <div id="tableInfo"></div>
</div>
<div data-options="region:'east', title:'查询条件', hideCollapsedContent:false, collapsible:true, collapsed:true" style="width:250px">
    <!--查询条件元素-->
    <form id="searchFormInfo" style="display: none">
        <div class="searchLabel">创建人：<input id="s_createUserId" name="createUserId"/></div>
        <div class="searchLabel">品牌：<input id="s_brand" name="brand"/></div>
        <div class="searchLabel">发票税点：<input id="s_billPoint" name="billPoint"/></div>
        <div class="searchLabel">公司主体：<input id="s_hostCompany" name="hostCompany"/></div>
        <div class="searchLabel">状态：<input id="s_status" name="status"/></div>
    </form>
</div>
<script type="text/javascript">
    //准备函数
    $(function () {
        initSearchForm();
        initGrid();
        getGridHidden("tableInfo");
        query(pageParam);
    });

    //查询条件初始化
    function initSearchForm() {
        var isSupervisorAll = "0";
        //获取系统配置信息（）
        ajaxGetFun("config/getConfigByText", {text: "is_supervisor_all"}, function (bizData) {
            if (bizData != null) {
                isSupervisorAll = bizData.configValue;
            }
        }, null, "获取系统配置信息", false);
        var roleType = sessionStorage.getItem("roleType");
        var isSup = sessionStorage.getItem("isSup");
        $("#s_createUserId").combobox({
            width: 170,
            panelHeight: "auto",
            valueField: "id",
            textField: "name",
            editable: true,
            url: roleType === "GLB" || roleType === "CWB" || (isSupervisorAll === "1" && isSup === "true") ? "user/listUserByRoleCode?roleCode=ALL" : "user/listSubordinateUser?hasSelf=true",
            method: "GET",
            loadFilter: function (data) {
                var temp = data.bizData;
                if (temp.length > 1) {
                    temp.unshift({"id":"ALL", "name":"全部"});
                }
                return temp;
            }
        });
        $("#s_brand").textbox({
            width: 183
        });
        $("#s_billPoint").combobox({
            width: 160,
            panelHeight: "auto",
            editable: false,
            data: billPointDataAll
        });
        $("#s_hostCompany").textbox({
            width: 160
        });
        $("#s_status").combobox({
            width: 183,
            panelHeight: "auto",
            editable: false,
            data: customerStatusDataAll
        });
        $("#searchFormInfo").show();
    }

    //表格元素初始化
    function initGrid() {
        $("#tableInfo").datagrid({
            pagination: true,
            singleSelect: true,
            fitColumns: true,
            striped: true,
            fit: true,
            columns: [[
                {field: "ck", checkbox: "true"},
                {field: "id", title: "编号", width: 60, align: "center", sortable: true},
                {field: "brand", title: "品牌", width: 100, align: "center", sortable: true},
                {field: "billPoint", title: "发票税点", width: 100, align: "center", sortable: true, formatter: billPointFormatter},
                {field: "hostCompany", title: "公司主体", width: 150, align: "center", sortable: true},
                {field: "hostName", title: "联系人", width: 100, align: "center", sortable: true},
                {field: "hostContact", title: "联系方式", width: 100, align: "center"},
                {field: "createUserId", title: "创建人", width: 100, align: "center", sortable: true, formatter: createUserIdFormatter},
                {field: "createDate", title: "创建时间", width: 130, align: "center", sortable: true, formatter: dateTimeFormatter},
                {field: "createDesc", title: "备注", width: 150, align: "left", sortable: false, formatter: remarkFormatter},
                {field: "status", title: "状态", width: 60, align: "center", sortable: true, formatter: customer_status_formatter},
                {field: "oper", title: "操作", width: 60, align: "center",
                    formatter: function (value, row) {
                        var operHtml = '';
                        if (row.status === 1) {
                            operHtml += '<a style="text-decoration:none" onClick="disableCustomer(\''+ row.id + '\')" title="停用"><i class="Hui-iconfont">&#xe631;</i></a>';
                        } else if (row.status  === 2) {
                            operHtml += '<a style="text-decoration:none" onClick="enableCustomer(\''+ row.id + '\')" title="启用"><i class="Hui-iconfont">&#xe615;</i></a>';
                        }
                        operHtml += '&emsp;<a style="text-decoration:none" onClick="setUserCustomer(\''+ row.id + '\')" title="授权"><i class="Hui-iconfont">&#xe62b;</i></a>';
                        return operHtml;
                    }
                }
            ]],
            toolbar: ".breadcrumb, #buttonInfo, #searchInfo",
            pageNumber: pageParam.pageNo,
            pageSize: pageParam.pageSize,
            pageList: Page.getPageList(),
            onSortColumn: function (orderBy, sortBy) {
                pageParam.pageNo = 1;
                pageParam.orderBy = orderBy;
                pageParam.sortBy = sortBy;
                query(pageParam);
            },
            onSelect: function() {
                selectedTableRow();
            },
            onUnselect: function() {
                selectedTableRow();
            },
            onSelectAll: function() {
                selectedTableRow();
            },
            onUnselectAll: function() {
                selectedTableRow();
            },
            onLoadSuccess: function () {
                toolBarBtnDisabledById([false, false, true, true], "buttonInfo");
            }
        }).datagrid("getPager").pagination({
            buttons: [
                {text: "个性化设置", iconCls: "icon-tip", handler: function() {selectColumnMenu("tableInfo");}}
            ],
            onSelectPage: function(pageNumber, pageSize) {
                pageParam.pageNo = pageNumber;
                pageParam.pageSize = pageSize;
                query(pageParam);
            },
            onChangePageSize: function(pageSize) {
                pageParam.pageSize = pageSize;
            }
        });
        //表格事件
        var selectedTableRow = function() {
            var selectRow = $("#tableInfo").datagrid("getSelections");
            if (selectRow.length === 0) {
                toolBarBtnDisabledById([false, false, true, true], "buttonInfo");
            } else if (selectRow.length === 1) {
                toolBarBtnDisabledById([false, false, false, false], "buttonInfo");
            } else {
                toolBarBtnDisabledById([false, false, true, false], "buttonInfo");
            }
        };
    }

    //分页查询
    function query(pageParam, goHead) {
        pageParam.condition = getFormJson($("#searchFormInfo"));
        if (goHead && goHead)
            pageParam.pageNo = 1;
        ajaxGetFun("customer/queryCustomerPage", pageParam, function(bizData) {
            $("#tableInfo").datagrid("loadData", bizData.rowList);
            $("#tableInfo").datagrid("getPager").pagination({pageNumber: bizData.pageNo, total: bizData.total});
        }, null, "获取客户列表");
    }

    //新增
    function addCustomer() {
        openIframe(false, 500, 400, "customer-info.html", null, function(index, layero) {
            var formEle = layero.find("iframe")[0].contentWindow.$("#formInfo");
            if (formEle.form("validate")) {
                ajaxPostFun("customer/saveCustomerInfo", getFormJson(formEle), function() {
                    layer.close(index);
                    query(pageParam, true);
                    showSuccessMsg("新增成功");
                }, null, "新建客户");
            }
        });
    }

    //修改
    function editCustomer() {
        var selectRows = $("#tableInfo").datagrid("getSelections");
        if (selectRows.length !== 1) {
            return showWarnMsg("请选择一条条目后操作");
        }
        openIframe(false, 500, 400, "customer-info.html", function() {
            $("iframe")[0].contentWindow.$("#formInfo").form("load", selectRows[0])
        }, function(index, layero) {
            var formEle = layero.find("iframe")[0].contentWindow.$("#formInfo");
            if (formEle.form("validate")) {
                ajaxPostFun("customer/updateCustomerInfo", getFormJson(formEle), function() {
                    layer.close(index);
                    query(pageParam);
                    showSuccessMsg("修改成功");
                }, null, "修改客户");
            }
        });
    }

    //删除
    function delCustomer() {
        var selectRows = $("#tableInfo").datagrid("getSelections");
        if (selectRows.length === 0) {
            return showWarnMsg("请选择条目后操作");
        }
        var ids = mergedRowCell(selectRows, "id");
        layer.confirm("确认要删除选中的客户吗？", function(index) {
            var successFun = function(bizData) {
                showSuccessMsg("已删除"+ bizData +"条数据");
                query(pageParam, true);
            };
            ajaxPostFun("customer/deleteCustomerInfoByIds", {ids: ids}, successFun, null, "删除客户");
        });
    }

    //停用
    function disableCustomer(customerId) {
        ajaxPostFun("customer/setCustomerDisable", {customerId: customerId}, function() {
            showSuccessMsg("停用成功");
            query(pageParam);
        }, null, "停用客户");
    }

    //启用
    function enableCustomer(customerId) {
        ajaxPostFun("customer/setCustomerEnable", {customerId: customerId}, function() {
            showSuccessMsg("启用成功");
            query(pageParam);
        }, null, "启用客户");
    }

    //授权（将客户授权给销售）
    function setUserCustomer(customerId) {
        var html = '<form id="formInfo"><table class="formTable">';
        //获取销售员工列表
        ajaxGetFun("user/listAllUsersByCustomerId", {customerId: customerId}, function (bizData) {
            var count = 0;
            for (var i=0; i<bizData.length; i=i+2) {
                if (i === bizData.length - 1) {
                    html +=
                        '<tr>' +
                        '   <td style="width: 150px; text-align: right">'+ bizData[i].name +'：</td>' +
                        '   <td style="width: 100px"><input type="checkbox" name="tableColSwitch" value="'+ bizData[i].id +'" class="lcs_check"'+ (bizData[i].isCustomerUser ? ' checked="checked"' : '') +' /></td>' +
                        '   <td style="width: 150px; text-align: right"></td>' +
                        '   <td style="width: 100px"></td>' +
                        '   <td style="width: 50px;"></td>' +
                        '</tr>';
                } else {
                    html +=
                        '<tr>' +
                        '   <td style="width: 150px; text-align: right">'+ bizData[i].name +'：</td>' +
                        '   <td style="width: 100px"><input type="checkbox" name="tableColSwitch" value="'+ bizData[i].id +'" class="lcs_check"'+ (bizData[i].isCustomerUser ? ' checked="checked"' : '') +' /></td>' +
                        '   <td style="width: 150px; text-align: right">'+ bizData[i+1].name +'：</td>' +
                        '   <td style="width: 100px"><input type="checkbox" name="tableColSwitch" value="'+ bizData[i+1].id +'" class="lcs_check"'+ (bizData[i+1].isCustomerUser ? ' checked="checked"' : '') +' /></td>' +
                        '   <td style="width: 50px;"></td>' +
                        '</tr>';
                }
                count++;
            }
            html +=  '</table></form>';
            var height = 65 + count*35;
            openHtml(false, 500, height, html, function () {
                $("input[name='tableColSwitch']").lc_switch();
            }, function(index, layero) {
                var userIds = "";
                $.each($("input[name='tableColSwitch']:checkbox"),function() {
                    if ($(this)[0].checked) {
                        userIds += ($(this).val() + ",");
                    }
                });
                if (userIds !== "") {
                    userIds = userIds.substr(0, userIds.length-1);
                }
                ajaxPostFun("customer/saveCustomerUser", {customerId: customerId, userIds: userIds}, function() {
                    showSuccessMsg("授权成功");
                    layer.close(index);
                }, null, "保存客户员工授权");
            });
        }, null, "根据客户ID获取所有员工");
    }
</script> 
</body>
</html>